package com.mms.utsc.smartnet;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {

	Button loginBtn;
	Button quitBtn;
	EditText etUsername;
	EditText etPassword;
	User user;
	ArrayList<MenuListItem> courses = new ArrayList<MenuListItem>();
	Handler toastHandler, alertHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		loginBtn = (Button) findViewById(R.id.bLogin);
		quitBtn = (Button) findViewById(R.id.bQuit);
		etUsername = (EditText) findViewById(R.id.etUsername);
		etPassword = (EditText) findViewById(R.id.etPassword);
		etUsername.setText("david");
		etPassword.setText("david.xing");

		loginBtn.setOnClickListener(this);
		quitBtn.setOnClickListener(this);
		
	}

	public void onClick(View view) {
		// TODO Auto-generated method stub
		VarHolder.setCurrContext(this);
		switch (view.getId()) {
		case R.id.bLogin:
			final ProgressDialog pd = ProgressDialog.show(this, "Log in", "Logging in - Please wait...");

			Thread loginThread = new Thread(new Runnable() {
				public void run() {
					DBHandler db = new DBHandler(null, getBaseContext());
					try {
						Looper.prepare();
						toastHandler = new Handler() {
							public void handleMessage(Message msg) {
								Toast.makeText(pd.getContext(), msg.toString(), Toast.LENGTH_SHORT).show();
							}
						};
						alertHandler = new Handler() {
							public void handleMessage(Message msg) {
								AlertDialog alert = (AlertDialog) msg.obj;
								alert.show();
							}
						};
						
						user = db.verifyLogin(etUsername.getText().toString(), etPassword.getText().toString());
						if (user != null) {
							VarHolder.setCurrUser(user);
							Message msg = new Message();
							msg.obj = "Login successful.";
							toastHandler.sendMessage(msg);
							courses = db.getCourseList();
							if (courses != null) {
								user.setCourses(courses);
							}
							Class<?> ourClass = Class.forName(VarHolder.getPackageName() + "MainMenu");
							Intent mainMenuIntent = new Intent(getBaseContext(), ourClass);
							startActivity(mainMenuIntent);
							pd.dismiss();
							toastHandler.getLooper().quit();
							alertHandler.getLooper().quit();
							db.close();
							finish();
						} else {
							//Error message shown in DBHandler
							/*
							pd.dismiss();
							final AlertDialog alert = new AlertDialog.Builder(pd.getContext()).create();
							alert.setButton(AlertDialog.BUTTON_NEGATIVE, "OK", new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									alert.dismiss();
								}
							});
							alert.setTitle("Login failed");
							alert.setMessage("Invalid username or password");

							Message msg = new Message();
							msg.obj = alert;
							alertHandler.sendMessage(msg);
							
							*/
							pd.dismiss();
							db.close();
							Looper.loop();
						}
						/*
						 * ArrayList<MenuListItem> courses = new
						 * DBHandler(pd).getCourseList(); if(courses != null)
						 * user.setCourses(courses);
						 */

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			loginThread.start();
			break;
		case R.id.bQuit:
			finish();
			break;
		}
	}

}
